﻿


$(function () {


    text_counter();
    on_update_panel_server(function () {
        autoaccount('#Txtpost', '');
        text_counter();
    });

    var $tree = create_tree({
        layer: "#demo1",
        tree_type_id: var_server.tree_type_id,
        webservice_url: var_server.webservice_url,
        root_folder_id: var_server.root_folder_id,
        icons_path: var_server.icons_path,
        on_select_node: function (e, data) {
            $('#HidSelectFolderId').val(data.rslt.obj.attr("id"));
            $('#BtnReload_UpPElaborations').click();
            //close_ajax_wait(null);
        },
        on_dnd: function (r) {
            $('#BtnReload_UpPElaborations').click();
            //close_ajax_wait(null);
        }

    });


    $("#BtnTwitterSearch").click(function () {
        //alert('uuu');
        $.oauthpopup({
            path: 'post_search?q=' + encodeURIComponent($('#TxtTwitterSearch').val()),
            callback: function () {
                //window.$.fancybox.showLoading();
                //window.location.href = 'useraccounts.aspx';
            },
            windowName: "Twitter",
            windowOptions: "location=0,status=0,width=1040,height=600"
        });
    });

    $("#add_folder").click(function () {
        $tree.jstree("create", null, "last", {
            attr: {
                rel: this.id.toString().replace("add_", "")
            }
        });
    });

    $("#rename_folder").click(function () {
        $tree.jstree("rename");
    });

    $("#remove_folder").click(function () {
        //$TreeElabFolders.jstree("select",'node_0');
        //$TreeElabFolders.jstree("deselect_all");
        $tree.jstree("remove");
    });

    $("#ChkAllElab").on("click", function () {
        c = $(this).attr('checked');
        $("#Grid_check input:checkbox").attr('checked', c);
    });


});


function text_counter() {
    $max = 140;

    $('#Txtpost').keyup(function () {
        $len2 = $('#Txtpost').val().length;
        $('#countBox').text($max - $len2)
        if ($max - $len2 < 0) {
            $str = $('#Txtpost').val();
            $str = $str.substring(0, $max)
            $('#Txtpost').val($str)
            $('#countBox').text(0)
        }
    });
}

function split(val) {
    return val.split(/ \s*/);
}
function extractLast(term) {
    return split(term).pop();
}

function autoaccount(l, h) {

    

    $(l).bind("keydown", function (event) {
        if (event.keyCode === $.ui.keyCode.TAB &&
                    $(this).data("ui-autocomplete").menu.active) {
            event.preventDefault();
        }
    }).autocomplete({
        minLength: 0,
        source: function (request, response) {
            PageMethods.set_path('/App/QueryAnalisys.aspx')
            PageMethods.AutoCompleteHashTags(
                        extractLast(request.term),
                        function OnSucceeded(result, usercontext, methodName) {
                            //alert(result);            
                            response(result);
                        });
        },
        focus: function () {
            // prevent value inserted on focus
            return false;
        },
        select: function (event, ui) {
            var terms = split(this.value);
            // remove the current input
            terms.pop();
            // add the selected item
            terms.push(ui.item.value);
            // add placeholder to get the comma-and-space at the end
            terms.push("");
            this.value = terms.join(" ");
            return false;
        }
    });

}

var chk_elab;
function onmousedown_chk_elab(o) {

    c = o.children('input:checkbox')
    chk_elab = c.attr('checked');
    c.attr('checked', true);
    //c.attr('checked', ! c.attr('checked') );
}

function onmouseup_chk_elab(o) {

    c = o.children('input:checkbox')
    c.attr('checked', !chk_elab);
}


